package com.itcast.yygh.hosp.mongo;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * mongodb测试常用的方法
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoTest {
    //引入mongodb模板的依赖
    @Autowired
    private MongoTemplate mongoTemplate;

    //添加数据
    @Test
    public void insertUser(){
        User user = new User();
        user.setName("刘备");
        user.setAge(45);
        user.setEmail("liubei@163.com");
        user.setCreateDate("2025-7-2");
        User user1 = mongoTemplate.insert(user);
        System.out.println(user1);
    }
    //查询所有数据
    @Test
    public void findAll(){
        List<User> users = mongoTemplate.findAll(User.class);
        System.out.println(users);
    }

    //多个条件查询
    @Test
    public void findUserByName(){
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is("刘备").and("age").is(45));
        //调用方法
        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println(users);

    }
    //模糊查询
    @Test
    public void findNames(){
        String name="s";
        String format = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(format,Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("name").regex(pattern));

        List<User> users = mongoTemplate.find(query, User.class);
        System.out.println(users);
    }
    //分页查询
    @Test
    public void findPages(){
        //当前页
        int pageNo = 1;
        //每页显示的条数
        int pageSize = 2;
        Query query = new Query();

        int count = (int)mongoTemplate.count(query, User.class);
        //通过mongo中skip进行数据索引的跳转  limit指定每页显示的数据
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("list", users);
        pageMap.put("totalCount",count);
        System.out.println(pageMap);
    }
    //修改数据
    @Test
    public void updateUser(){
        //查询
        User user = mongoTemplate.findById("68540af46a051aa5358016d2", User.class);
        user.setName("关羽");
        user.setEmail("guanyu@163.com");
        user.setCreateDate("2025-7-2");
        //构建条件
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        //通过update设置对应值
        Update update = new Update();
        update.set("name",user.getName());
        update.set("email",user.getEmail());
        update.set("createDate",user.getCreateDate());

        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);
        long modifiedCount = upsert.getModifiedCount();
        System.out.println(modifiedCount);
    }
    //删除数据
    @Test
    public void delUser(){
        //构建删除的条件
        Query query = new Query(Criteria.where("_id").is("68540aeb6a051aa5358016d1"));
        DeleteResult remove = mongoTemplate.remove(query, User.class);
        long deletedCount = remove.getDeletedCount();
        System.out.println(deletedCount);
    }

}
